package com.anniljing.kotlionlearn.Basic.scopeFunc

fun main() {
    testLetHandleNUll()
//    testWithBasic()
//    testAlsoAndApply()
}

/**
 * 使用let函数处理需要针对一个可null的对象统一做判空处理
 */
fun testLetHandleNUll(){
    var result: String? =null
//    result="Let"
    result?.run {
        println(this)
    }
}

fun testWithBasic(){
    var user=User("张三",18)
    val result=with(user){
        println("my name is $name, I am $age years old")
        1000
    }
    println(result)

}

data class User(var name:String,var age:Int)

/**
 * let和run返回的则为闭包
 */
fun testLetAndRun() {
    val result = "HelloLet".let {
        println(it)
        "HelloWorld"
    }
    println(result)

    val resultRun="HelloRun".run {
        println(this)
        "HelloWorld"
    }
    println(resultRun)

    val user=User("李四",28)
    user.run {
        name="王五"
    }

}
/**
 * also和apply返回的都是该对象
 */
fun testAlsoAndApply(){
    var user=User("张三",18)
    var result=user.apply {
        name="李四"
        age=20
    }
    println(result)

    result=user.also {
        it.name="王五"
        it.age=28
    }
    println(result)
}
